package com.shqcjd.yunweiyanoperator.helper.rxjavahelper;


import com.lzy.okgo.exception.HttpException;
import com.lzy.okgo.exception.StorageException;

import java.io.IOException;
import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * user：lqm
 * desc：自己的Observer，减少实现不必要的回调
 */
public abstract class RxObserver<T>  implements Observer<T> {

    @Override
    public void onSubscribe(Disposable d){
        _onSubscribe(d);
    }

    @Override
    public void onNext(T t) {
        _onNext(t);
    }
    @Override
    public void onError(Throwable e) {
        if(e!=null) e.printStackTrace();
        if (e instanceof UnknownHostException || e instanceof ConnectException) {
            _onError("网络连接失败,请连接网络!");
        } else if (e instanceof SocketTimeoutException) {
            _onError("网络请求超时");
        } else if (e instanceof HttpException || e instanceof IOException) {
            _onError("服务器未响应");
        } else if (e instanceof StorageException) {
            _onError("sd卡不存在或者没有权限!");
        }else {
            _onError(e.getMessage());
        }
    }
    @Override
    public void onComplete() {
        _onComplete();
    }

    public void _onSubscribe(Disposable d) {

    }
    public void _onComplete() {

    }

    //抽象方法，必须实现
    public abstract void _onNext(T t);
    public abstract void _onError(String errorMessage);

}